#include "Player.h"
#include "Game.h"

void Player::update(float frameTime)
{
	const InputHandler* inputHandler = game->getInputHandler();
	Game* instance = (Game*)game;
	
	// Get
	std::list<Node*> connectedNodes = m_baseNode->getConnectedNodes();
	connectedNodes.push_back(m_prevNode); //add previous node as well

	bool moveToNext = false;

	if(this->m_baseNode->getNodeState() == Node::PLAYER || 
		this->m_baseNode->getHackingState() == Node::HACKED || 
		m_currentNode->getHackingState() == Node::HACKED || 
		m_currentNode->getNodeState() == Node::PLAYER)
	{
		moveToNext = true;
	}

	if(moveToNext)
	{
		
		if (!inputHandler->wasKeyDown(DIK_RIGHT) && inputHandler->isKeyDown(DIK_RIGHT)){
			std::list<Node*>::iterator iter = connectedNodes.begin();
			while (iter != connectedNodes.end()){
				Node* temp = (*iter);
				D3DXVECTOR3 nodePos = temp->getPosition();

				if (getPosition().y == nodePos.y && getPosition().x < nodePos.x && m_baseNode != *iter)
				{
					m_prevNode = m_baseNode;
					this->m_baseNode =  *iter;
					this->m_currentNode = *iter;
				}
				iter++;
			}
		
		}else if (!inputHandler->wasKeyDown(DIK_LEFT) && inputHandler->isKeyDown(DIK_LEFT) ){
			std::list<Node*>::iterator iter = connectedNodes.begin();
			while (iter != connectedNodes.end()){
				Node* temp = (*iter);
				D3DXVECTOR3 nodePos = temp->getPosition();

				if (getPosition().y == nodePos.y && getPosition().x > nodePos.x && m_baseNode != *iter)
				{
					m_prevNode = m_baseNode;
					this->setBaseNode( *iter);
					this->m_currentNode = *iter;
				}
				iter++;
			}
		}else if (!inputHandler->wasKeyDown(DIK_DOWN) && inputHandler->isKeyDown(DIK_DOWN)){
			std::list<Node*>::iterator iter = connectedNodes.begin();
			while (iter != connectedNodes.end()){
				Node* temp = (*iter);
				D3DXVECTOR3 nodePos = temp->getPosition();

				if (getPosition().y < nodePos.y && getPosition().x == nodePos.x && m_baseNode != *iter)
				{
					m_prevNode = m_baseNode;
					this->setBaseNode( *iter);
					this->m_currentNode = *iter;
				}
				iter++;
			}
		}else if(!inputHandler->wasKeyDown(DIK_UP) && inputHandler->isKeyDown(DIK_UP)){
			std::list<Node*>::iterator iter = connectedNodes.begin();
			while (iter != connectedNodes.end()){
				Node* temp = (*iter);
				D3DXVECTOR3 nodePos = temp->getPosition();

				if (getPosition().y > nodePos.y && getPosition().x == nodePos.x && m_baseNode != *iter)
				{
					m_prevNode = m_baseNode;
					this->setBaseNode( *iter);
					this->m_currentNode = *iter;
				}
				iter++;
			}
		}

		moveToNext = false;
	}else{
		//cannot move forward only backwards
		if (!inputHandler->wasKeyDown(DIK_RIGHT) && inputHandler->isKeyDown(DIK_RIGHT)){
			std::list<Node*>::iterator iter = connectedNodes.begin();
			while (iter != connectedNodes.end()){
				Node* temp = (*iter);
				D3DXVECTOR3 nodePos = temp->getPosition();

				if (getPosition().y == nodePos.y && getPosition().x < nodePos.x && m_prevNode == *iter)
				{
					m_prevNode = m_baseNode;
					this->m_baseNode =  *iter;
					this->m_currentNode = *iter;
				}
				iter++;
			}
		
		}else if (!inputHandler->wasKeyDown(DIK_LEFT) && inputHandler->isKeyDown(DIK_LEFT) ){
			std::list<Node*>::iterator iter = connectedNodes.begin();
			while (iter != connectedNodes.end()){
				Node* temp = (*iter);
				D3DXVECTOR3 nodePos = temp->getPosition();

				if (getPosition().y == nodePos.y && getPosition().x > nodePos.x && m_baseNode != *iter && m_prevNode == *iter)
				{
					m_prevNode = m_baseNode;
					this->setBaseNode( *iter);
					this->m_currentNode = *iter;
				}
				iter++;
			}
		}else if (!inputHandler->wasKeyDown(DIK_DOWN) && inputHandler->isKeyDown(DIK_DOWN)){
			std::list<Node*>::iterator iter = connectedNodes.begin();
			while (iter != connectedNodes.end()){
				Node* temp = (*iter);
				D3DXVECTOR3 nodePos = temp->getPosition();

				if (getPosition().y < nodePos.y && getPosition().x == nodePos.x && m_baseNode != *iter && m_prevNode == *iter)
				{
					m_prevNode = m_baseNode;
					this->setBaseNode( *iter);
					this->m_currentNode = *iter;
				}
				iter++;
			}
		}else if(!inputHandler->wasKeyDown(DIK_UP) && inputHandler->isKeyDown(DIK_UP)){
			std::list<Node*>::iterator iter = connectedNodes.begin();
			while (iter != connectedNodes.end()){
				Node* temp = (*iter);
				D3DXVECTOR3 nodePos = temp->getPosition();

				if (getPosition().y > nodePos.y && getPosition().x == nodePos.x && m_baseNode != *iter && m_prevNode == *iter)
				{
					m_prevNode = m_baseNode;
					this->setBaseNode( *iter);
					this->m_currentNode = *iter;
				}
				iter++;
			}
		}
	}
	anims->playAnimation("Focus");
	setPosition(m_currentNode->getPosition());

	if (inputHandler->isKeyDown(DIK_SPACE) || inputHandler->isKeyDown(DIK_RETURN))
	{
		if (m_baseNode->getHackingState() != Node::HACKED && m_baseNode->getHackingState() != Node::HACKING )
		{
			Message message(this->m_currentNode, "Hacking", 0);
			MessageHandler::sendMessage(message);
		}
	}

	if (m_currentNode == m_baseNode)
		anims->playAnimation("Focus");

	/** Update animations and particle. Every frame. */
	//anims->update(frameTime);
	//particle.update(frameTime);

	//position = particle.position;
}


void Player::draw(ID3DXSprite* sprite)
{
	//First we work out the centre of the frame
	D3DXVECTOR2 centre;
	centre.x = (float)(anims->getCurrentAnimation()->getFrameWidth() / 2);
	centre.y = (float)(anims->getCurrentAnimation()->getFrameHeight() / 2);

	/* Now we create a transformation matrix. If flipped, we scale X by -1
	 * we also use the position vector in our transaction */
	D3DXMATRIX mat;
	/*if(flip)
		D3DXMatrixTransformation2D(&mat, &centre, 0,
								   &D3DXVECTOR2(-1, 1), 0, 0,
								   &D3DXVECTOR2(position.x, position.y));
	else */
		D3DXMatrixTransformation2D(&mat, 0, 0, 0, 0, 0,
								   &D3DXVECTOR2(position.x, position.y));

	//Now we use this transformation to draw with, we set the draw position to 0 to avoid issues
	
 	anims->draw(sprite, D3DXVECTOR3(0, 0, 0), mat);

}

void Player::handleMessage(Message message)
{
	// HANDLE MESSAGES...
	if (message.message == "NodeSecured")
	{
		Node* node = (Node*)message.data;
		
		for (int i = 0; i < m_hackedNodes.size(); ++i)
		{
			if (m_hackedNodes.front() == node)
			{
				m_hackedNodes.pop_front();
				i = m_hackedNodes.size();
			}
			else
				m_hackedNodes.pop_front();
		}
		// Implement knocking nodes off hacked list.
	}

	if (message.message == "NodeHacked")
	{
		Node* node = (Node*)message.data;
		
		
		if (node->getOwner() == Node::FINISH)
		{
		
			anims->playAnimation("Idle");
			m_hackedNodes.push_front(node);
			//m_baseNode = node;
		}
		else
		{
			if (node != m_hackedNodes.front())
			{
				m_hackedNodes.push_front(node);
				//m_baseNode = node;
			}
		}
	}

	if (message.message == "EarnMoney")
	{
		Node* node = (Node*)message.data;
		if(!node->canStillMoney())
		{
			if(node->getOwner() == node->FINISH)
			{
				this->score += 0;
			}else if(node->getOwner() == node->NEUTRAL)
			{
				this->score += 59193;
			}
			node->setStillMoney(true);
		}
	}


	if(message.message == "LoseMoney")
	{
		Node* node = (Node*)message.data;
		if(node->canStillMoney())
		{
			if(node->getOwner() == node->FINISH)
			{
				this->score -= 69495;
			}else if(node->getOwner() == node->NEUTRAL)
			{
				this->score -= 991923;
			}else if(node->getOwner() == node->NEUTRAL)
			{
				this->score -= 12093712039;
			}
			node->setStillMoney(false);
		}
	}

}